/************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     uart.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief     
 *************************************************************************/

#include "uart.h"
#include <string.h>
#include "bsp.h"
#include "pit.h"
#include "dma.h"
#include "lt168_sci.h"

#include "usb.h"



uint8_t page_refreshing = 0;
/********************************************************CRC check*************************************************/

// CRC value of high byte
const uint8_t auchCRCHi[] = {
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
	0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
	0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
	0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
	0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
	0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
	0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
	0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
	0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
	0x40};

// CRC value of low byte
const char auchCRCLo[] = {
	0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
	0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
	0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
	0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
	0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
	0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
	0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
	0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
	0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
	0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
	0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
	0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
	0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
	0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
	0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
	0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
	0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
	0x40};

/* The function returns CRC with uint16_t type*/
uint16_t CRC16(uint8_t *puchMsg, uint16_t usDataLen)
{
	uint8_t uchCRCHi = 0xFF;			// CRC high byte initialization
	uint8_t uchCRCLo = 0xFF;			// CRC low byte initialization
	uint16_t uIndex;		 			// CRC query table index
	while (usDataLen--)					// Complete the entire message buffer
	{
		uIndex = uchCRCLo ^ *puchMsg++;	// calculate CRC
		uchCRCLo = uchCRCHi ^ auchCRCHi[uIndex];
		uchCRCHi = auchCRCLo[uIndex];
	}
	return (uchCRCHi << 8 | uchCRCLo);
}

// 8位校验和
uint8_t CheckSum8(uint8_t *data, uint8_t len) {
    uint32_t sum = 0; // 使用32位变量避免溢出
    for (uint8_t i = 0; i < len; i++) {
        sum += data[i]; // 累加所有字节
    }
    uint8_t raw_sum = (uint8_t)(sum & 0xFF); // 取低8位
    uint8_t checksum = ~raw_sum + 1; // 取反加1
    return checksum;
}
//void Uart_Init(uint32_t pclk, uint32_t bound)
//{
//	uint32_t band_rate = 0;
//	SCI3->SCIBRDF = (((pclk * 8 / bound) + 1) / 2) & 0x003f;
//	band_rate = (pclk * 4 / bound) >> 6;
//	SCI3->SCIBDH = (uint8_t)((band_rate >> 8) & 0x00ff);
//	SCI3->SCIBDL = (uint8_t)(band_rate & 0x00ff);

//	SCI3->SCICR2 |= (1 << 2) | (1 << 3) | (1 << 5);	// enable receive, send enable and receive interrupt	
//	//	NVIC_Init(0, 0, SCI1_IRQn, 2);
//}

/*****************************uart communication drive*****************************/
//#include "dmac_drv.h"

#if (UARTBUS_OPTION == 1)
//DMA_CHANNEL_REG *sci_dma_channel[2][DMAC_CHNUM] = {{(DMA_CHANNEL_REG *)(DMA1_BASE_ADDR), (DMA_CHANNEL_REG *)(DMA1_BASE_ADDR + 0x58),
//													(DMA_CHANNEL_REG *)(DMA1_BASE_ADDR + 0xB0), (DMA_CHANNEL_REG *)(DMA1_BASE_ADDR + 0x108)},
//												   {(DMA_CHANNEL_REG *)(DMA2_BASE_ADDR), (DMA_CHANNEL_REG *)(DMA2_BASE_ADDR + 0x58),
//													(DMA_CHANNEL_REG *)(DMA2_BASE_ADDR + 0xB0), (DMA_CHANNEL_REG *)(DMA2_BASE_ADDR + 0x108)}}; // global struct variable for for Channel registers
//
//DMA_CONTROL_REG *sci_dma_control[2] = {(DMA_CONTROL_REG *)(DMA1_BASE_ADDR + 0x2C0),
//									   (DMA_CONTROL_REG *)(DMA2_BASE_ADDR + 0x2C0)}; // global struct variable for for DMAC registers
#endif

void UartTFT_SCI_Init(sci *UARTx, uint32_t bound)
{
#if 0
	uint8_t dmaNum = 0;
	uint8_t n = 0;

    uint32_t tempReg = 0;
	uint16_t SBR = 0;
	uint32_t BAUDRATE = bound;

	if(UARTx == SCI0)
	{
		CCM->EPORT2FCR &= (~0x03);
	    EIC->U32_IER.IE_SCI0 = 1;          //锟叫讹拷锟斤拷锟斤拷
	}
	else if(UARTx == SCI1)
	{
		CCM->EPORT2FCR &= (~0x0C);
	    EIC->U32_IER.IE_SCI1 = 1;          //锟叫讹拷锟斤拷锟斤拷
	}


	SBR = Find_SCI_Optimal_SBR(BAUDRATE);
	Bit_Clear(UARTx->BAUD,SCI_BAUD_SBR_MASK);      // Baud rate = IPS/((OSR+1)*SBR) ,1<SBR<8191
    Bit_Set(UARTx->BAUD,SCI_BAUD_SBR(SBR));
    UARTx->OSR = SCI_OSR( SYS_CLOCK / ((SBR) * BAUDRATE) - 1);// 3<=OSR<=255锟斤拷锟斤拷锟叫★拷锟�锟斤拷岜伙拷锟轿�5

//    UARTx->BAUD &=~(1<<29);     // 10bit
//    UARTx->CTRL &=~(1<<2);     // Parity Enable

    Bit_Set(UARTx->FIFO,SCI_FIFO_TXFE_MASK); // 锟斤拷锟酵伙拷锟斤拷 FIFO使锟斤拷
    									  // Transmit FIFO/Buffer depth = 8 datawords;
    tempReg |= SCI_WATER_TXWATER(0);     // 锟斤拷锟酵伙拷锟斤拷水印锟侥达拷锟斤拷
    UARTx->WATER |= tempReg;

    Bit_Set(UARTx->FIFO,SCI_FIFO_RXFE_MASK); // Receive FIFO enable
                                         // Transmit FIFO/Buffer depth = 8 datawords;
    tempReg |= SCI_WATER_RXWATER(0);    //
    UARTx->WATER |= tempReg;

//    Bit_Set(UARTx->FIFO,SCI_FIFO_RXUFE_MASK);// SCI_FIFO_RXUFE_MASK

    Bit_Set(UARTx->CTRL,SCI_CTRL_TE_MASK); // 锟斤拷锟斤拷使锟斤拷
    Bit_Set(UARTx->CTRL,SCI_CTRL_RE_MASK); // 锟斤拷锟斤拷使锟斤拷
//    Bit_Set(UARTx->CTRL,SCI_CTRL_TIE_MASK); // 锟斤拷锟斤拷锟叫讹拷使锟斤拷
    Bit_Set(UARTx->CTRL,SCI_CTRL_RIE_MASK); // 锟斤拷锟斤拷锟叫讹拷使锟斤拷

//    EIC->U32_IER.IE_SCI1 = 1;   //锟叫讹拷锟斤拷锟斤拷

#if (UARTBUS_OPTION == 1)
	UARTx->SCIFCR |= SCIFCR_TFEN;
	UARTx->SCIDCR |= SCIDCR_TXDMAE_MASK; // Uart_DMA enable

	COMuart_dma_control->DMA_CHEN &= (~(CHANNEL_WRITE_ENABLE(n) | CHANNEL_ENABLE(n))); // DMA disable

	//	COMuart_dma_control->DMA_CONFIG = 0;
	COMuart_dma_channel[n]->DMA_DADDR = (uint32_t)&UARTx->SCIDRL;
	COMuart_dma_channel[n]->DMA_CTRL = DNOCHG | SIEC | M2P_DMA;
	//	COMuart_dma_control->DMA_CONFIG = 1;

	COMuart_dma_channel[n]->DMA_CFG = (HS_SEL_SRC_SOFT) | (HS_SEL_DST_HARD) | (FIFO_EMPTY) | (CH_PRIOR2);
	COMuart_dma_channel[n]->DMA_CFG_HIGH = COMuart_DST_TX; // SRC_PER_SCI3_TX|DST_PER_SCI3_TX;
#endif

//	if(parity_flag & 0x01)	//CJC
//	{
//		if(parity_flag & 0x02)
//			UARTx->SCICR1 |= (1 << 0);	//Odd parity
//		else
//			UARTx->SCICR1 &=~(1 << 0);	//even parity check
//		UARTx->SCICR1 |= (1 << 1) | (1 << 4);	//Turn on checksum to set an 11-bit frame
//	}
//	UARTx->SCICR2 |= (1 << 2) | (1 << 3) | (1 << 5);	// enable receive, send enable and receive interrupt

//	NVIC_Init(0, 0, SCI3_IRQn, 2);



#else


	uint16_t SBR = 0;

	EIC->U32_IER.IE_SCI1 = 0;
	UARTx->CTRL = 0x0;

	SBR = Find_SCI_Optimal_SBR(bound);
	Bit_Clear(UARTx->BAUD,SCI_BAUD_SBR_MASK);      // Baud rate = IPS/((OSR+1)*SBR) ,1<SBR<8191
    Bit_Set(UARTx->BAUD,SCI_BAUD_SBR(SBR));
    UARTx->OSR = SCI_OSR( SYS_CLOCK / ((SBR) * bound) - 1);// 3<=OSR<=255锟斤拷锟斤拷锟叫★拷锟�锟斤拷岜伙拷锟轿�5

    Bit_Set(UARTx->CTRL,SCI_CTRL_ORIE_MASK); // 锟斤拷锟斤拷卸锟绞癸拷锟�
    Bit_Set(UARTx->CTRL,SCI_CTRL_RIE_MASK);  // 锟斤拷锟斤拷锟叫讹拷使锟斤拷

    Bit_Set(UARTx->CTRL,SCI_CTRL_ILIE_MASK); //锟竭匡拷锟斤拷锟叫讹拷使锟斤拷
    Bit_Set(UARTx->CTRL,SCI_CTRL_ILT_MASK);  //锟斤拷锟叫达拷停止位锟斤拷始锟斤拷锟斤拷
    Bit_Set(UARTx->CTRL,SCI_CTRL_IDLECFG(3));//锟斤拷锟叫讹拷锟劫ｏ拷3-8锟斤拷4-16锟斤拷5-32锟斤拷2锟斤拷n锟轿凤拷锟斤拷锟斤拷锟街凤拷

    Bit_Set(UARTx->FIFO,SCI_FIFO_RXFE_MASK); //锟斤拷锟斤拷FIFO使锟斤拷
    Bit_Set(UARTx->WATER,SCI_WATER_RXWATER(7));

    //	UARTx->U32SCIFIFO.RXFE = 0; //锟斤拷锟斤拷FIFO使锟斤拷
    //	UARTx->U32SCIWATER.RXWATER = 0;

    Bit_Set(UARTx->CTRL,SCI_CTRL_RE_MASK); //锟斤拷锟斤拷使锟斤拷
    Bit_Set(UARTx->CTRL,SCI_CTRL_TE_MASK);  //锟斤拷锟斤拷使锟斤拷



/*
	UARTx->U32SCIBAUD.SBR = 1;   //Baud rate = IPS/((OSR+1)*SBR) ,1<SBR<8191
	UARTx->U32SCIOSR.OSR  = SYS_CLK/((SCI1->U32SCIBAUD.SBR)*tBaudRate) - 1; // 3<=OSR<=255锟斤拷锟斤拷锟叫★拷锟�锟斤拷岜伙拷锟轿�5

	UARTx->U32SCICTRL.ORIE = 1; //锟斤拷锟斤拷卸锟绞癸拷锟�
	UARTx->U32SCICTRL.RIE = 1;  //锟斤拷锟斤拷锟叫讹拷使锟斤拷


	UARTx->U32SCICTRL.ILIE = 1; //锟竭匡拷锟斤拷锟叫讹拷使锟斤拷
	UARTx->U32SCICTRL.ILT = 1;  //锟斤拷锟叫达拷停止位锟斤拷始锟斤拷锟斤拷
	UARTx->U32SCICTRL.IDLECFG = 3; //锟斤拷锟叫讹拷锟劫ｏ拷3-8锟斤拷4-16锟斤拷5-32锟斤拷2锟斤拷n锟轿凤拷锟斤拷锟斤拷锟街凤拷

	UARTx->U32SCIFIFO.RXFE = 1; //锟斤拷锟斤拷FIFO使锟斤拷
	UARTx->U32SCIWATER.RXWATER = 7;

//	UARTx->U32SCIFIFO.RXFE = 0; //锟斤拷锟斤拷FIFO使锟斤拷
//	UARTx->U32SCIWATER.RXWATER = 0;

	UARTx->U32SCICTRL.RE = 1;    //锟斤拷锟斤拷使锟斤拷
	UARTx->U32SCICTRL.TE = 1;    //锟斤拷锟斤拷使锟斤拷

//	DelayMs(5);
*/

	EIC->U32_IER.IE_SCI1 = 1;  //enable SCI0 interrupt

#endif
}

void LT_ClearRxBuf(void)
{
	uint16_t i = 0;
	for (i = 0; i < RX_SIZE; i++)
	{
		gUsartRx.Buf[i] = 0;
	}
}

// uart feedback -- package frame header(Levetop protocol) + data + length + CRC
void LT_SendData_CRC_Frame(uint8_t *buf, uint8_t len)
{
#if (UARTBUS_OPTION == 0)
	/*uint16_t i = 0;
	uint16_t TxToPc_crc;
	uint8_t crc[2] = {0};

	*(buf + 0) = SCI_C0;
	*(buf + 1) = SCI_C1;
	if (CRC_Enable_Flag)
	{
		TxToPc_crc = CRC16(buf + 3, len);
		crc[0] = (uint8_t)(TxToPc_crc & 0x00ff);
		crc[1] = (uint8_t)((TxToPc_crc >> 8) & 0x00ff);

		len += 2;

		*(buf + len + 1) = crc[0];
		*(buf + len + 2) = crc[1];
	}
	*(buf + 2) = len;

	for (i = 0; i < 3 + len; i++)//for (i = 0; i < len; i++)
	{
		while(Bit_Read(COMuart->STAT,SCI_STAT_TDRE_MASK) ==0);
		COMuart->DATA = (*(buf + i));
	}*/
    uint16_t i;
    uint8_t checksum;

    // 帧头
    buf[0] = SCI_C0;
    buf[1] = SCI_C1;

    // 计算校验和（范围：buf[3] 到 buf[3+len-1]）
    checksum = CheckSum8(buf + 3, len);

    // 数据长度（不需要额外增加校验和长度，因为此处假设长度字段只包含数据）
    buf[2] = len+1;

    // 添加校验和（1字节）
    buf[3 + len] = checksum;

    // 发送数据（总长度：3头 + len数据 + 1校验和）
    for (i = 0; i < 3 + len + 1; i++) {
        while(Bit_Read(COMuart->STAT, SCI_STAT_TDRE_MASK) == 0);
        COMuart->DATA = buf[i];
    }
	// For debug
	// sci_dma_control[0]->DMA_CHEN &= ~(CHANNEL_WRITE_ENABLE(2)|CHANNEL_ENABLE(2));//DMA disable//
	// sci_dma_channel[0][2]->DMA_SADDR = (uint32_t)buf;
	// sci_dma_channel[0][2]->DMA_CTRL_HIGH = len+3;
	//
	// //sci_dma_control[0]->DMA_MASKTFR |= CHANNEL_UMASK(2);
	// sci_dma_control[0]->DMA_CHEN |= (CHANNEL_WRITE_ENABLE(2)|CHANNEL_ENABLE(2));

#endif
	
#if (UARTBUS_OPTION == 5)
	uint16_t i = 0;
		uint16_t TxToPc_crc;
		uint8_t crc[2] = {0};
		uint16_t count1,count2;
		uint8_t rebuf[512] = {0};
		*(buf + 0) = SCI_C0;
		*(buf + 1) = SCI_C1;
		if (CRC_Enable_Flag)
		{
			TxToPc_crc = CRC16(buf + 3, len);
			crc[0] = (uint8_t)(TxToPc_crc & 0x00ff);
			crc[1] = (uint8_t)((TxToPc_crc >> 8) & 0x00ff);

			len += 2;

			*(buf + len + 1) = crc[0];
			*(buf + len + 2) = crc[1];
		}
		*(buf + 2) = len;

		#if 1
		count1 = (3 + len)  / 64;
		count2 = ((3 + len) % 64);


		for (i = 0;i < count1; i++)
		{
			EP_IN_Transfer(EP2,(uint8_t *)gu8EP2_IN_Buffer,EP2_SIZE,&buf[64 * i],64);	//hid锟斤拷1.0只锟斤拷64bytes锟斤拷锟酵斤拷锟斤拷
			DelayUS(1200);
		}
		if (count2 > 0)
		{
			memset(&rebuf,0,64);
			memcpy(rebuf,&buf[64 * count1],count2);
			EP_IN_Transfer(EP2,(uint8_t *)gu8EP2_IN_Buffer,EP2_SIZE,rebuf,64);
			DelayUS(1200);

		}
		#elif USB_MODE_2D0

		count1 = (3 + len) / 512;
		count2 = (3 + len) % 512;




		for (i = 0;i < count1; i++)
		{
			USB_HID_EP_TX_ISR(INDEX_EP2,&buf[512 * i],512);
			DelayUS(1200);
		}
		if (count2 > 0)
		{

			memset(&rebuf,0,512);
			memcpy(rebuf,&buf[512 * count1],count2);
			USB_HID_EP_TX_ISR(INDEX_EP2,rebuf,512);
		}
		#endif


#endif

#if (UARTBUS_OPTION == 6)
	uint16_t i = 0;
	uint16_t TxToPc_crc;
	uint8_t crc[2] = {0};

	*(buf + 0) = SCI_C0;
	*(buf + 1) = SCI_C1;
	if (CRC_Enable_Flag)
	{
		TxToPc_crc = CRC16(buf + 3, len);
		crc[0] = (uint8_t)(TxToPc_crc & 0x00ff);
		crc[1] = (uint8_t)((TxToPc_crc >> 8) & 0x00ff);

		len += 2;

		*(buf + len + 1) = crc[0];
		*(buf + len + 2) = crc[1];
	}
	*(buf + 2) = len;


//	while(USB->INT_STAT & USB_STAT_TOKDNE)
//	{
//		USB->U32_CTL.ODD_RST = 1;
//		USB_Handler0();
//		USB->U32_INT_STAT.TOKDNE = 1;
//	}
	EP_IN_Transfer(EP2,(uint8_t *)gu8EP2_IN_Buffer,EP2_SIZE, buf, 3 + len);
	DelayMS(1);
#endif
}

// only for single reg addr remap
void LT_InfoFeedback_with_addr(uint8_t *buf, uint8_t len)
{
#if (UARTBUS_OPTION == 0)
	uint16_t i = 0;
	uint16_t TxToPc_crc;
	uint8_t crc[2] = {0};

	*(buf + 0) = SCI_C0;
	*(buf + 1) = SCI_C1;
	if (CRC_Enable_Flag)
	{
		TxToPc_crc = CRC16(buf + 3, len);
		crc[0] = (uint8_t)(TxToPc_crc & 0x00ff);
		crc[1] = (uint8_t)((TxToPc_crc >> 8) & 0x00ff);

		len += 2;

		*(buf + len + 1) = crc[0];
		*(buf + len + 2) = crc[1];
	}
	*(buf + 2) = len;

	Var_AddrToReg(&buf[4]);
	
	for (i = 0; i < 3 + len; i++)
	{
		while(Bit_Read(COMuart->STAT,SCI_STAT_TDRE_MASK) ==0);
		COMuart->DATA = (*(buf + i));
	}
#endif
}

void LT_SendFlashData_CRC_Frame(uint8_t *buf, uint16_t len)
{

#if (UARTBUS_OPTION == 0)
	uint16_t i = 0;
	uint16_t TxToPc_crc;
	uint8_t crc[2] = {0};

	*(buf + 0) = SCI_C0;
	*(buf + 1) = SCI_C1;
	*(buf + 2) = 0;
	if (CRC_Enable_Flag)
	{
		TxToPc_crc = CRC16(buf + 5, len);
		crc[0] = (uint8_t)(TxToPc_crc & 0x00ff);
		crc[1] = (uint8_t)((TxToPc_crc >> 8) & 0x00ff);
		
		len += 2;
		*(buf + len + 3) = crc[0];
		*(buf + len + 4) = crc[1];
	}
	*(buf + 3) = (len >> 8) & 0xFF;
	*(buf + 4) = len & 0xFF;
	for (i = 0; i < 5 + len; i++)
	{
		while(Bit_Read(COMuart->STAT,SCI_STAT_TDRE_MASK) ==0);
		COMuart->DATA = (*(buf + i));
	}
#endif

#if (UARTBUS_OPTION == 5)
	
	uint16_t i = 0;
	uint16_t TxToPc_crc;
	uint8_t crc[2] = {0};
	uint8_t count1,count2;

	*(buf + 0) = SCI_C0;
	*(buf + 1) = SCI_C1;
	*(buf + 2) = 0;
	if (CRC_Enable_Flag)
	{
		TxToPc_crc = CRC16(buf + 5, len);
		crc[0] = (uint8_t)(TxToPc_crc & 0x00ff);
		crc[1] = (uint8_t)((TxToPc_crc >> 8) & 0x00ff);
		
		len += 2;
		*(buf + len + 3) = crc[0];
		*(buf + len + 4) = crc[1];
	}
	*(buf + 3) = (len >> 8) & 0xFF;
	*(buf + 4) = len & 0xFF;

	
	#if 1
	count1 = (5 + len)  / 64;
	count2 = ((5 + len) % 64);

	for (i = 0;i < count1; i++)
	{
//		USB_HID_EP_TX_ISR(INDEX_EP2,&buf[64 * i],64);
		EP_IN_Transfer(EP2,(uint8_t *)gu8EP2_IN_Buffer,EP2_SIZE,&buf[64 * i],64);
		DelayUS(1200);
	}
	if (count2 > 0)
	{
		memset(&buf[64 * count1 + count2],0,64 - count2);
//		USB_HID_EP_TX_ISR(INDEX_EP2,&buf[64 * count1],64);
		EP_IN_Transfer(EP2,(uint8_t *)gu8EP2_IN_Buffer,EP2_SIZE,&buf[64 * count1],64);
	}
	#elif USB_MODE_2D0
	
	count1 = (5 + len)  / 512;
	count2 = ((5 + len) % 512);


	for (i = 0;i < count1; i++)
	{
		USB_HID_EP_TX_ISR(INDEX_EP2,&buf[512 * i],512);
		DelayUS(1200);
	}
	if (count2 > 0)
	{
		memset(&buf[512 * count1 + count2],0,512 - count2);
		USB_HID_EP_TX_ISR(INDEX_EP2,&buf[512 * count1],512);
	}
	#endif

#endif
	
#if (UARTBUS_OPTION == 6)
	uint16_t i = 0;
	uint16_t TxToPc_crc;
	uint8_t crc[2] = {0};

	*(buf + 0) = SCI_C0;
	*(buf + 1) = SCI_C1;
	*(buf + 2) = 0;
	if (CRC_Enable_Flag)
	{
		TxToPc_crc = CRC16(buf + 5, len);
		crc[0] = (uint8_t)(TxToPc_crc & 0x00ff);
		crc[1] = (uint8_t)((TxToPc_crc >> 8) & 0x00ff);
		
		len += 2;
		*(buf + len + 3) = crc[0];
		*(buf + len + 4) = crc[1];
	}
	*(buf + 3) = (len >> 8) & 0xFF;
	*(buf + 4) = len & 0xFF;

	EP_IN_Transfer(EP2,(uint8_t *)gu8EP2_IN_Buffer,EP2_SIZE, buf, 5 + len);
	DelayMS(1);
#endif
}

// for modbus 
// Uart feedback -- Packaging data + CRC             
void LT_SendData_CRC_NoFrame(uint8_t *buf, uint8_t len) 
{
	uint16_t i = 0;
	uint16_t TxToPc_crc;
	uint8_t crc[2] = {0};

	TxToPc_crc = CRC16(buf, len);
	crc[1] = (uint8_t)((TxToPc_crc >> 8) & 0x00ff);
	crc[0] = (uint8_t)(TxToPc_crc & 0x00ff);
	*(buf + len) = crc[0];
	*(buf + len + 1) = crc[1];

#if (UARTBUS_OPTION == 2)
	for (i = 0; i < len + 2; i++)
	{
		while(Bit_Read(COMuart->STAT,SCI_STAT_TDRE_MASK) ==0);
		COMuart->DATA = (*(buf + i));
	}
#endif
#if (UARTBUS_OPTION == 1)
//	COMuart_dma_control->DMA_CHEN &= ~(CHANNEL_WRITE_ENABLE(COMuart_CHNUM) | CHANNEL_ENABLE(COMuart_CHNUM)); // DMA disable
//	COMuart_dma_channel[COMuart_CHNUM]->DMA_SADDR = (uint32_t)buf;
//	COMuart_dma_channel[COMuart_CHNUM]->DMA_CTRL_HIGH = len + 2;
//
//	// sci_dma_control[0]->DMA_MASKTFR |= CHANNEL_UMASK(2);
//	COMuart_dma_control->DMA_CHEN |= (CHANNEL_WRITE_ENABLE(COMuart_CHNUM) | CHANNEL_ENABLE(COMuart_CHNUM));

	for (i = 0; i < 2 + len; i++)
	{
		while(Bit_Read(COMuart->STAT,SCI_STAT_TDRE_MASK) ==0);
		COMuart->DATA = (*(buf + i));
	}

#endif
}

/*****************************Levetop protocol as slave*****************************/
#if (UARTBUS_OPTION == 0 || UARTBUS_OPTION == 4)
uint8_t Rx_Buffer_short[256] = {0};
uint8_t Rx_Buffer_long[4096 + 2] = {0};

uint16_t Rx_Count_short = 0;
uint16_t Rx_Count_long = 0;
uint16_t Rx_Num = 0;
uint16_t Respond_Num = 0;
uint16_t Respond_Count = 0;

uint8_t Respond_C0_OK = 0;
uint8_t Respond_C1_OK = 0;
uint8_t Respond_Length = 0;
uint8_t Respond_Flag = 0;
uint16_t Respond_FlashLength = 0;

#if (UARTBUS_OPTION == 0)
void SCI1_Handler(void)
{
#if 0
	uint8_t ch = 0;
	if(Bit_Read(SCI1->STAT,SCI_STAT_RDRF_MASK))
	{
		ch = (SCI1->DATA &0xFF );
		if (Rx_Count_long > 4096 + 2)
			Rx_Count_long = 0;
		Rx_Buffer_long[Rx_Count_long++ % RX_SIZE] = ch;
		Rx_Num++;
//		LTPrintf("ch  x%x  \r\n", ch);
	}
#endif

#if 1
	// FIFO --------------------------------------------------------------------
	uint16_t i;
	UINT8 temp_num = 0;
	UINT32 temp0,temp1;

	temp0 = SCI1->STAT;
	temp1 = SCI1->CTRL;
//	uartint_flag = 1;
//	if(SCI1->U32SCISTAT.RDRF && SCI1->U32SCICTRL.RIE)
	if((temp0&(1<<21)) && (temp1&(1<<21)))
	{
//		temp_num = SCI1->U32SCIWATER.RXCOUNT;
		temp_num = (SCI1->WATER>>24)&0xFF;
		for(i = 0;i<temp_num;i++)
		{
			if (Rx_Count_long > 4096 + 2)
				Rx_Count_long = 0;
			Rx_Buffer_long[Rx_Count_long++ % RX_SIZE] = (SCI1->DATA &0xFF );
			Rx_Num++;
		}
	}

//	if(SCI1->U32SCISTAT.OR && SCI1->U32SCICTRL.ORIE)
	if((temp0&(1<<19)) && (temp1&(1<<27)))
	{
//		LTPrintf("锟斤拷锟絓r\n");
//		SCI1->U32SCISTAT.OR = 1;
		SCI1->STAT |= (1<<19);

	}

//	if(SCI1->U32SCISTAT.IDLE)  //锟斤拷锟斤拷锟叫讹拷锟斤拷锟絉X DMA锟斤拷锟斤拷锟斤拷锟斤拷,锟角憋拷要锟斤拷要锟侥讹拷
	if(temp0&(1<<20))  //锟斤拷锟斤拷锟叫讹拷锟斤拷锟絉X DMA锟斤拷锟斤拷锟斤拷锟斤拷,锟角憋拷要锟斤拷要锟侥讹拷
	{
		SCI1->STAT |= (1<<20);   //clear IDLE
//		temp_num = SCI1->U32SCIWATER.RXCOUNT;
		temp_num = (SCI1->WATER>>24)&0xFF;

		for(i = 0;i<temp_num;i++)
		{
			if (Rx_Count_long > 4096 + 2)
				Rx_Count_long = 0;
			Rx_Buffer_long[Rx_Count_long++ % RX_SIZE] = (SCI1->DATA &0xFF );
			Rx_Num++;
		}
	}
#endif
}
#endif

// Get each frame of data from the uart port receiving buffer
uint8_t crc_valid = 0;
void Main_From_UartRxBuff(void)
{
    uint16_t i = 0;
    if (Respond_Num < Rx_Num)
    {
        for (i = 0; i < (Rx_Num - Respond_Num); i++)
        {
            uint8_t current_byte = Rx_Buffer_long[(i + Respond_Count) % RX_SIZE];

            // 优先检测帧头，无论当前是否在解析中
            if (!Respond_C0_OK && current_byte == SCI_C0)
            {
                // 发现新帧头，重置接收状态
                Respond_C0_OK = 1;
                Respond_C1_OK = 0;
                gUsartRx.Count = 0;
                Respond_Flag = 0;
                continue; // 跳过后续处理，继续检测C1
            }

            if (Respond_C0_OK && !Respond_C1_OK && current_byte == SCI_C1)
            {
                // 完整帧头，开始接收数据
                Respond_C1_OK = 1;
                Respond_Flag = 1;
                gUsartRx.Count = 0;
                continue;
            }

            // 如果当前不在接收数据状态，跳过处理
            if (!Respond_C1_OK) continue;

            // 数据接收处理
            gUsartRx.Buf[gUsartRx.Count++] = current_byte;

            // 协议解析状态机
            if (Respond_Flag == 1)
            {
                Respond_Flag = 0;
                Respond_Length = current_byte;
                if (Respond_Length == 0)
                {
                    Respond_Flag = 2;
                }
            }
            else if (Respond_Length == 0)
            {
                if (Respond_Flag == 2)
                {
                    Respond_FlashLength = current_byte;
                    Respond_Flag = 3;
                }
                else if (Respond_Flag == 3)
                {
                    Respond_FlashLength = (Respond_FlashLength << 8) + current_byte + 2;
                    Respond_Flag = 0;
                }
            }

            // 检查是否接收完整
            uint8_t frame_complete = 0;
            if ((Respond_Length != 0 && gUsartRx.Count > Respond_Length) ||
                (Respond_Length == 0 && Respond_Flag == 0 && gUsartRx.Count > Respond_FlashLength))
            {
                // 执行CRC校验
                uint8_t crc_valid = 0;
                if (Respond_Length != 0)
                {
                    uint8_t data_len = Respond_Length - 1;
                    uint8_t *data_start = &gUsartRx.Buf[1];
                    uint8_t received_crc = gUsartRx.Buf[Respond_Length];
                    uint8_t calculated_crc = CheckSum8(data_start, data_len);

                    LTPrintf("[CRC] Calc: 0x%02X, Recv: 0x%02X\n", calculated_crc, received_crc);

                    if (calculated_crc == received_crc) {
                        crc_valid = 1;

                        // 确保数据长度足够
                        if (gUsartRx.Count < 5) {
                            //LTPrintf("[ERROR] Incomplete frame!\n");
                            return;
                        }

                        // 提取命令码（命令码位于 Buf[1] 和 Buf[2]）
                        uint8_t cmd_high = gUsartRx.Buf[1];
                        uint8_t cmd_low  = gUsartRx.Buf[2];

                        //LTPrintf("[CMD] High: 0x%02X, Low: 0x%02X\n", cmd_high, cmd_low);

                        if (cmd_high == 0x80 && cmd_low == 0x0A) {
                            // 提取参数（假设参数位于 Buf[3] 和 Buf[4]）
                            File_countB = ((uint16_t)gUsartRx.Buf[3] << 8) | gUsartRx.Buf[4];
                            set_Filelevel = gUsartRx.Buf[5];
                            Page_sum1 = 1;
                            File_level = 1;
                            //LTPrintf("[CMD] File_countB set to 0x%04X\n", File_countB);
                        }
                        if (cmd_high == 0x10 && cmd_low == 0x02&&gUsartRx.Buf[3]==0x0C) {
                        	var[0x0403*2+1] =gUsartRx.Buf[5];
                        }
                    }
                }
                else
                {
                    // 扩展帧校验（根据需求实现）
                    //LTPrintf("[WARN] Extended Frame CRC Check Not Implemented!\n");
                    crc_valid = 1; // 示例值，根据实际情况修改
                }

                // 无论校验是否通过都重置状态
                frame_complete = 1;
                gUsartRx.Flag = crc_valid ? 1 : 0;

                // 强制重置接收状态以检测后续帧
                Respond_C0_OK = 0;
                Respond_C1_OK = 0;
                gUsartRx.Count = 0;
                Respond_Flag = 0;
            }

            // 如果帧处理完成，继续处理后续字节（不移除i++）
            if (frame_complete) break;
        }
        Respond_Count = (i + Respond_Count) % RX_SIZE;
        Respond_Num += i;
    }
    else if (Respond_Num == Rx_Num && Rx_Num != 0)
    {
        Respond_Num = 0;
        Rx_Num = 0;
        Respond_Count = 0;
        Rx_Count_long = 0;
    }
}
/*void Main_From_UartRxBuff(void)
{
	uint16_t i = 0;
	if (Respond_Num < Rx_Num)
	{
		for (i = 0; i < (Rx_Num - Respond_Num); i++)
		{
			if (Respond_C1_OK)
			{
				gUsartRx.Buf[gUsartRx.Count++] = Rx_Buffer_long[(i + Respond_Count) % RX_SIZE];
				if (Respond_Flag == 1)
				{
					Respond_Flag = 0;
					Respond_Length = Rx_Buffer_long[(i + Respond_Count) % RX_SIZE];
					if (Respond_Length == 0)
					{
						Respond_Flag = 2;
					}
				}
				else if (Respond_Length == 0)
				{
					if (Respond_Flag == 2)
					{
						Respond_FlashLength = Rx_Buffer_long[(i + Respond_Count) % RX_SIZE];
						Respond_Flag = 3;
					}
					else if(Respond_Flag == 3)
					{
						Respond_FlashLength = (Respond_FlashLength << 8) + Rx_Buffer_long[(i + Respond_Count) % RX_SIZE] + 2;
						Respond_Flag = 0;
					}
				}

				if ((gUsartRx.Count > Respond_Length && Respond_Length != 0) || (gUsartRx.Count > Respond_FlashLength && Respond_Length == 0 && Respond_Flag == 0))
				{
					gUsartRx.Flag = 1;
					Respond_C1_OK = 0;
					Respond_C0_OK = 0;
					i++;
					break;
				}
			}
			if (Respond_C0_OK && !Respond_C1_OK && Rx_Buffer_long[(i + Respond_Count) % RX_SIZE] != SCI_C1)
			{
				Respond_C0_OK = 0;
				gUsartRx.Count = 0;
			}

			if (!Respond_C0_OK && Rx_Buffer_long[(i + Respond_Count) % RX_SIZE] == SCI_C0)
			{
				Respond_C0_OK = 1;
				gUsartRx.Count = 0;
			}
			if (!Respond_C1_OK && Rx_Buffer_long[(i + Respond_Count) % RX_SIZE] == SCI_C1 && Respond_C0_OK)
			{
				Respond_C1_OK = 1;
				Respond_Flag = 1;
				gUsartRx.Count = 0;
			}
		}
		Respond_Count = (i + Respond_Count) % RX_SIZE;
		Respond_Num += i;
	}
	else if (Respond_Num == Rx_Num && Rx_Num != 0)
	{
		Respond_Num = 0;
		Rx_Num = 0;
		Respond_Count = 0;
		Rx_Count_long = 0;
	}
}*/

// CAN接收帧结构体
/*typedef struct {
    uint32_t id;     // CAN标识符
    uint8_t  data[8];// 数据域
    uint8_t  len;    // 数据长度
} CanFrame;

#define CAN_RX_BUFFER_SIZE 32
volatile CanFrame canRxBuffer[CAN_RX_BUFFER_SIZE];
volatile uint16_t canRxHead = 0, canRxTail = 0;

// 协议相关定义
#define CMD_HEAD_ID   0x701       // 命令头帧ID
#define DATA_FRAME_ID 0x702       // 数据帧ID
#define CMD_C0        0xC0        // 协议控制字符
#define CMD_C1        0xC1

void Main_From_CANRxBuff(void)
{
    while (canRxTail != canRxHead) {  // 处理所有接收到的CAN帧
        CanFrame frame = canRxBuffer[canRxTail];
        canRxTail = (canRxTail + 1) % CAN_RX_BUFFER_SIZE;

        if (frame.id == CMD_HEAD_ID && frame.len >= 2) {
            // 处理命令头帧
            if (frame.data[0] == CMD_C0 && frame.data[1] == CMD_C1) {
                Respond_C0_OK = 1;
                Respond_C1_OK = 1;
                gUsartRx.Count = 0;
                Respond_Flag = 1;

                // 解析长度字段
                if (frame.len >= 3) {
                    Respond_Length = frame.data[2];
                    if (Respond_Length == 0 && frame.len >= 5) {
                        Respond_FlashLength = (frame.data[3] << 8) | frame.data[4];
                        Respond_Flag = 3;
                    }
                }
            }
        }
        else if (Respond_C1_OK && frame.id == DATA_FRAME_ID) {
            // 处理数据帧
            uint8_t copyLen = frame.len;
            if ((gUsartRx.Count + copyLen) > sizeof(gUsartRx.Buf)) {
                copyLen = sizeof(gUsartRx.Buf) - gUsartRx.Count;
            }

            memcpy(&gUsartRx.Buf[gUsartRx.Count], frame.data, copyLen);
            gUsartRx.Count += copyLen;

            // 检查数据包完整性
            if ((Respond_Length != 0 && gUsartRx.Count >= Respond_Length) ||
                (Respond_Length == 0 && gUsartRx.Count >= Respond_FlashLength))
            {
                gUsartRx.Flag = 1;
                Respond_C0_OK = 0;
                Respond_C1_OK = 0;
            }
        }
    }
}

// CAN接收中断示例（需硬件适配）
void CAN_RX_IRQHandler(void)
{
    if (CAN_GetRxMsg(&frame)) {  // 获取CAN帧
        uint16_t nextHead = (canRxHead + 1) % CAN_RX_BUFFER_SIZE;
        if (nextHead != canRxTail) {
            canRxBuffer[canRxHead] = frame;
            canRxHead = nextHead;
        }
    }
}*/

#endif

#if (UARTBUS_OPTION == 1)
void SCI1_Handler(void)
{
#if 1
	// FIFO --------------------------------------------------------------------
	uint16_t i;
	UINT8 temp_num = 0;
	UINT32 temp0,temp1;

	temp0 = SCI1->STAT;
	temp1 = SCI1->CTRL;

//	if(SCI1->U32SCISTAT.RDRF && SCI1->U32SCICTRL.RIE)
	if((temp0>>21) && (temp1>>21))
	{
//		temp_num = SCI1->U32SCIWATER.RXCOUNT;
		temp_num = (SCI1->WATER>>24)&0xFF;
		for(i = 0;i<temp_num;i++)
		{

						if (gUsartRx.Count > 4096 + 2)
							gUsartRx.Count = 0;
						gUsartRx.Buf[gUsartRx.Count++] = (SCI1->DATA &0xFF );


						pit_1ms_count = 0;
						uart_flag = 1;
		}
	}

//	if(SCI1->U32SCISTAT.OR && SCI1->U32SCICTRL.ORIE)
	if((temp0>>19) && (temp1>>27))
	{
//		LTPrintf("锟斤拷锟絓r\n");
//		SCI1->U32SCISTAT.OR = 1;
		SCI1->STAT |= (1<<19);

	}

//	if(SCI1->U32SCISTAT.IDLE)  //锟斤拷锟斤拷锟叫讹拷锟斤拷锟絉X DMA锟斤拷锟斤拷锟斤拷锟斤拷,锟角憋拷要锟斤拷要锟侥讹拷
	if((temp0>>20))  //锟斤拷锟斤拷锟叫讹拷锟斤拷锟絉X DMA锟斤拷锟斤拷锟斤拷锟斤拷,锟角憋拷要锟斤拷要锟侥讹拷
	{
		SCI1->STAT |= (1<<20);   //clear IDLE
//		temp_num = SCI1->U32SCIWATER.RXCOUNT;
		temp_num = (SCI1->WATER>>24)&0xFF;

		for(i = 0;i<temp_num;i++)
		{
//			if (Rx_Count_long > 4096 + 2)
//				Rx_Count_long = 0;
//			Rx_Buffer_long[Rx_Count_long++ % RX_SIZE] = (SCI1->DATA &0xFF );
//			Rx_Num++;

						if (gUsartRx.Count > 4096 + 2)
							gUsartRx.Count = 0;
						gUsartRx.Buf[gUsartRx.Count++] = (SCI1->DATA &0xFF );


						pit_1ms_count = 0;
						uart_flag = 1;
		}
	}
#endif
}

#endif

/*****************************ModBus protocol as host*****************************/
#if (UARTBUS_OPTION == 2)
// uart interrupt reception and pit1 (5ms) interrupt cooperate to complete the acquisition of each frame data

void SCI1_Handler(void)
{
	// FIFO --------------------------------------------------------------------
	uint16_t i;
	UINT8 temp_num = 0;
	UINT32 temp0,temp1;

	temp0 = SCI1->STAT;
	temp1 = SCI1->CTRL;
	if((temp0>>21) && (temp1>>21))
	{
		temp_num = (SCI1->WATER>>24)&0xFF;
		for(i = 0;i<temp_num;i++)
		{
			if (gUsartRx.Count > 4096 + 2)
				gUsartRx.Count = 0;
			gUsartRx.Buf[gUsartRx.Count++] = (SCI1->DATA &0xFF );


			pit_1ms_count = 0;
			uart_flag = 1;
		}
	}

	if((temp0>>19) && (temp1>>27))
	{
		SCI1->STAT |= (1<<19);

	}

	if((temp0>>20))  //锟斤拷锟斤拷锟叫讹拷锟斤拷锟絉X DMA锟斤拷锟斤拷锟斤拷锟斤拷,锟角憋拷要锟斤拷要锟侥讹拷
	{
		SCI1->STAT |= (1<<20);   //clear IDLE
		temp_num = (SCI1->WATER>>24)&0xFF;

		for(i = 0;i<temp_num;i++)
		{

			if (gUsartRx.Count > 4096 + 2)
				gUsartRx.Count = 0;
			gUsartRx.Buf[gUsartRx.Count++] = (SCI1->DATA &0xFF );


			pit_1ms_count = 0;
			uart_flag = 1;
		}
	}

}
// char Master_Roll_buff[1600]={0};
//{
//	0xA5, 0x01, 0x03, 0x00,0x00, 0x00,0x46, 0x20,0x00, 0x00,0xc8,  0x05, 0x00, 0x00,0x00, 0x00,
//	0xA5, 0x01, 0x06, 0x00,0x41, 0x00,0x02, 0x20,0x41, 0x00,0x64,  0x05, 0x00, 0x00,0x00, 0x00,
//	0xA5, 0x01, 0x03, 0x00,0x23, 0x00,0x3C, 0x20,0x23, 0x00,0xc8,  0x05, 0x02, 0x30,0x35, 0x00,
//	0xA5, 0x01, 0x06, 0x00,0x02, 0x00,0x02, 0x20,0x02, 0x00,0xc8,  0x05, 0x03, 0x00,0x00, 0x00,
//	0xA5, 0x01, 0x06, 0x00,0x03, 0x00,0x02, 0x20,0x03, 0x00,0xc8,  0x05, 0x03, 0x00,0x01, 0x00
// };
/* Valid | Node | Instruction | Register address | Length | Variable address | Response time ms | Number of retransmissions | Instruction execution mode and corresponding parameters | Reserved

Command execution mode:
0x00 means that the command is executed on all pages, and the following parameters can be set to a fixed value of 0x0000;
0x01 indicates that the command is only executed on the specified page, and the parameter immediately following is set as the page ID number;
0x02 indicates that the corresponding pointer address data is 0x4C54 before the instruction is executed (the data of the pointer address will be cleared after execution), and the following parameter is set as the pointer address;
0x03 indicates that the command is executed only at the mark position 1 of the corresponding number of the customization mode, and the following parameter is the mark position number;
*/



volatile uint8_t tx_en_flag = 1;		// Allow sending flag bit
volatile uint8_t tx_repeat_flag = 0;	// Whether to resend flag bit
volatile uint8_t tx_repeat_cnt = 0;		// Cumulative number of retransmissions

volatile USART_TX_INFO gUsartTx;		// Modbus operation parameter information area

uint8_t Send_ModbusBuf[256] = {0};

volatile uint8_t Master_mode03_flag[100] = {0};	// Customized variables
volatile uint8_t Master_mode03_Var[200] = {0};	// Customized variables

// The transmission mechanism of host timing and repeated serial port data transmission
void Uart_cmd_Send(void)	//CJC
{
	uint8_t i = 0,j = 0;			
	uint16_t num=0, data_temp=0;
	uint8_t byte_temp = 0;	
	uint16_t sum=0, count=0, cnt=0;
	

	uint8_t k;


	if (tx_en_flag & tx_500ms_flag)	// Allowed to send and within the fixed period
	{

		if (Cnt_ModbusTX == 0)
			tx_10ms_cnt = 0;		// It is cleared only after entering the transmission

		if (tx_repeat_flag)
		{
			tx_repeat_cnt++;
			/* If the cumulative number of retransmissions exceeds the maximum or the broadcast mode is exceeded,
			skip and execute the next command */
			if (tx_repeat_cnt > gUsartTx.RP_Cnt || gUsartTx.Sla_Add == 0)
			{
				tx_repeat_cnt = 0;
				tx_repeat_flag = 0;
				if (gUsartTx.Mode == 0x02)		// Data clearing of parameter pointer address in mode 2
				{
					var[2 * (gUsartTx.Opt_Add)] = 0;
					var[2 * (gUsartTx.Opt_Add) + 1] = 0;
				}
				else if (gUsartTx.Mode == 0x03) // Customized Code_ The flag bit corresponding to mode 3 is cleared
				{
					Master_mode03_flag[gUsartTx.Opt_Add] = 0;
				}
			}
		}

		if (tx_repeat_flag)
		{
			tx_repeat_flag = 0;
			Cnt_ModbusTX -= 16;

		}
		else
		{
			tx_repeat_cnt = 0;
			if (Cnt_ModbusTX >= Sum_ModbusTX)	// After all instructions are executed, wait for the next timing cycle to start before sending again
			{
				Cnt_ModbusTX = 0;
				//				tx_500ms_flag = 0;
				if (tx_10ms_cnt <= 50)
					tx_500ms_flag = 0;

				return;
			}
		}

		gUsartTx.Flag    = var[MODBUS_ADDR + Cnt_ModbusTX + 0];
		gUsartTx.Sla_Add = var[MODBUS_ADDR + Cnt_ModbusTX + 1];
		gUsartTx.CMD     = var[MODBUS_ADDR + Cnt_ModbusTX + 2];
		gUsartTx.REG_Add = var[MODBUS_ADDR + Cnt_ModbusTX + 3] << 8 | var[MODBUS_ADDR + Cnt_ModbusTX + 4];
		gUsartTx.Len     = var[MODBUS_ADDR + Cnt_ModbusTX + 5] << 8 | var[MODBUS_ADDR + Cnt_ModbusTX + 6];
		gUsartTx.Var_Add = var[MODBUS_ADDR + Cnt_ModbusTX + 7] << 8 | var[MODBUS_ADDR + Cnt_ModbusTX + 8];
		gUsartTx.Timeout = var[MODBUS_ADDR + Cnt_ModbusTX + 9] << 8 | var[MODBUS_ADDR + Cnt_ModbusTX + 10];
		gUsartTx.RP_Cnt  = var[MODBUS_ADDR + Cnt_ModbusTX + 11];
		gUsartTx.Mode    = var[MODBUS_ADDR + Cnt_ModbusTX + 12];
		gUsartTx.Opt_Add = var[MODBUS_ADDR + Cnt_ModbusTX + 13] << 8 | var[MODBUS_ADDR + Cnt_ModbusTX + 14];

		if (gUsartTx.Flag == 0xA5)
		{
			if (gUsartTx.Mode == 0x00) // Mode 0
			{

				if (gUsartTx.CMD == 0x01)
				{
					Send_ModbusBuf[0] = gUsartTx.Sla_Add;
					Send_ModbusBuf[1] = gUsartTx.CMD;
					Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
					Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
					Send_ModbusBuf[4] = gUsartTx.Len >> 8;
					Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
					LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
					tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
					pit_1ms_count3 = 0;
					timeout_flag = 1;
				}
				else if (gUsartTx.CMD == 0x02)
				{
					Send_ModbusBuf[0] = gUsartTx.Sla_Add;
					Send_ModbusBuf[1] = gUsartTx.CMD;
					Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
					Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
					Send_ModbusBuf[4] = gUsartTx.Len >> 8;
					Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
					LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
					tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
					pit_1ms_count3 = 0;
					timeout_flag = 1;
				}				
				else if (gUsartTx.CMD == 0x03)
				{
					Send_ModbusBuf[0] = gUsartTx.Sla_Add;
					Send_ModbusBuf[1] = gUsartTx.CMD;
					Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
					Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
					Send_ModbusBuf[4] = gUsartTx.Len >> 8;
					Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
					LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
					tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
					pit_1ms_count3 = 0;
					timeout_flag = 1;
				}
				else if (gUsartTx.CMD == 0x04)
				{
					Send_ModbusBuf[0] = gUsartTx.Sla_Add;
					Send_ModbusBuf[1] = gUsartTx.CMD;
					Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
					Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
					Send_ModbusBuf[4] = gUsartTx.Len >> 8;
					Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
					LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
					tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
					pit_1ms_count3 = 0;
					timeout_flag = 1;
				}				
				else if (gUsartTx.CMD == 0x05)
				{
					Send_ModbusBuf[0] = gUsartTx.Sla_Add;
					Send_ModbusBuf[1] = gUsartTx.CMD;
					Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
					Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
					
					num = gUsartTx.REG_Add%16;				
					// data_temp = (var[(gUsartTx.Var_Add) * 2]<<8 | var[(gUsartTx.Var_Add) * 2 + 1]);		
					uint16_t addr_temp = gUsartTx.Var_Add;
					Var_AddrRemapping(&addr_temp); 
					data_temp = (var[(addr_temp) * 2]<<8 | var[(addr_temp) * 2 + 1]);				
					Send_ModbusBuf[4] = (data_temp & (1<<num))?0xFF:0x00;
					Send_ModbusBuf[5] = 0x00;
					LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
					tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
					pit_1ms_count3 = 0;
					timeout_flag = 1;
				}
				else if (gUsartTx.CMD == 0x06)
				{
					Send_ModbusBuf[0] = gUsartTx.Sla_Add;
					Send_ModbusBuf[1] = gUsartTx.CMD;
					Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
					Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
					// Send_ModbusBuf[4] = var[(gUsartTx.Var_Add) * 2];
					// Send_ModbusBuf[5] = var[(gUsartTx.Var_Add) * 2 + 1];
					uint16_t addr_temp = gUsartTx.Var_Add;
					Var_AddrRemapping(&addr_temp); 
					Send_ModbusBuf[4] = var[(addr_temp) * 2];
					Send_ModbusBuf[5] = var[(addr_temp) * 2 + 1];
					LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
					tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
					pit_1ms_count3 = 0;
					timeout_flag = 1;
				}
				else if (gUsartTx.CMD == 0x0F)
				{
					Send_ModbusBuf[0] = gUsartTx.Sla_Add;
					Send_ModbusBuf[1] = gUsartTx.CMD;
					Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
					Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
					Send_ModbusBuf[4] = gUsartTx.Len >> 8;
					Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
					Send_ModbusBuf[6] = gUsartTx.Len%8?(gUsartTx.Len/8+1):(gUsartTx.Len/8);										
					
					num = (gUsartTx.REG_Add+gUsartTx.Len-1)/16 - gUsartTx.REG_Add/16 + 1;
					count = 0;

					uint16_t addr_temp = gUsartTx.Var_Add;
					Var_AddrRemapping(&addr_temp); 

					for (i = 0; i < num; i++)
					{
						// data_temp = (var[(gUsartTx.Var_Add+i) * 2]<<8 | var[(gUsartTx.Var_Add+i) * 2 + 1]);
						data_temp = (var[(addr_temp+i) * 2]<<8 | var[(addr_temp+i) * 2 + 1]);
						if(i==0)
							sum = ( (gUsartTx.Len>=count+16)?16:(gUsartTx.Len-count) )-gUsartTx.REG_Add%16;
						else						
							sum = (gUsartTx.Len>=count+16)?16:(gUsartTx.Len-count);
						
						for(j = 0; j <sum; j++)
						{
							if(i==0)
							{
								if( data_temp&(1<<(j+gUsartTx.REG_Add%16)) )
									byte_temp |= (1<<(cnt%8));
								else
									byte_temp &=~(1<<(cnt%8));								
							}
							else
							{
								if( data_temp&(1<<j) )
									byte_temp |= (1<<(cnt%8));
								else
									byte_temp &=~(1<<(cnt%8));								
							}
							
							cnt++;
							if((cnt!=0 && cnt%8==0) || cnt>=gUsartTx.Len )
							{
								if(cnt%8==0)	
									Send_ModbusBuf[7 + cnt/8-1] = byte_temp;
								else	
									Send_ModbusBuf[7 + cnt/8] = byte_temp;
								byte_temp = 0;
							}									
						}
						count+=sum;								
					}

					LT_SendData_CRC_NoFrame(Send_ModbusBuf, 7+Send_ModbusBuf[6]);
					tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
					pit_1ms_count3 = 0;
					timeout_flag = 1;
				}				
				else if (gUsartTx.CMD == 0x10)
				{
					Send_ModbusBuf[0] = gUsartTx.Sla_Add;
					Send_ModbusBuf[1] = gUsartTx.CMD;
					Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
					Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
					Send_ModbusBuf[4] = gUsartTx.Len >> 8;
					Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
					Send_ModbusBuf[6] = gUsartTx.Len*2;

					uint16_t addr_temp = gUsartTx.Var_Add;
					Var_AddrRemapping(&addr_temp); 

					for (i = 0; i < gUsartTx.Len; i++)			
					{
						// Send_ModbusBuf[7 + 2 * i + 0] = var[(gUsartTx.Var_Add) * 2 + 2 * i + 0];
						// Send_ModbusBuf[7 + 2 * i + 1] = var[(gUsartTx.Var_Add) * 2 + 2 * i + 1];
						Send_ModbusBuf[7 + 2 * i + 0] = var[(addr_temp) * 2 + 2 * i + 0];
						Send_ModbusBuf[7 + 2 * i + 1] = var[(addr_temp) * 2 + 2 * i + 1];
					}
					LT_SendData_CRC_NoFrame(Send_ModbusBuf, 7 + Send_ModbusBuf[6]);	
					tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
					pit_1ms_count3 = 0;
					timeout_flag = 1;
				}
			}
			else if (gUsartTx.Mode == 0x01) // Mode 1
			{
				if (gUsartTx.Opt_Add == (var[VAR_PAGE * 2] << 8 | var[VAR_PAGE * 2 + 1]))
				{

					if (gUsartTx.CMD == 0x01)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x02)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}					
					else if (gUsartTx.CMD == 0x03)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x04)
				{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}					
					else if (gUsartTx.CMD == 0x05)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						
						num = gUsartTx.REG_Add%16;
						// data_temp = (var[(gUsartTx.Var_Add) * 2]<<8 | var[(gUsartTx.Var_Add) * 2 + 1]);					
						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 
						data_temp = (var[(addr_temp) * 2]<<8 | var[(addr_temp) * 2 + 1]);				
						Send_ModbusBuf[4] = (data_temp & (1<<num))?0xFF:0x00;
						Send_ModbusBuf[5] = 0x00;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x06)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						// Send_ModbusBuf[4] = var[(gUsartTx.Var_Add) * 2];
						// Send_ModbusBuf[5] = var[(gUsartTx.Var_Add) * 2 + 1];
						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 
						Send_ModbusBuf[4] = var[(addr_temp) * 2];
						Send_ModbusBuf[5] = var[(addr_temp) * 2 + 1];
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x0F)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						Send_ModbusBuf[6] = gUsartTx.Len%8?(gUsartTx.Len/8+1):(gUsartTx.Len/8);									
						
						num = (gUsartTx.REG_Add+gUsartTx.Len-1)/16 - gUsartTx.REG_Add/16 + 1;
						count = 0;

						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 

						for (i = 0; i < num; i++)
						{
							// data_temp = (var[(gUsartTx.Var_Add+i) * 2]<<8 | var[(gUsartTx.Var_Add+i) * 2 + 1]);
							data_temp = (var[(addr_temp+i) * 2]<<8 | var[(addr_temp+i) * 2 + 1]);
							if(i==0)
								sum = ( (gUsartTx.Len>=count+16)?16:(gUsartTx.Len-count) )-gUsartTx.REG_Add%16;
							else						
								sum = (gUsartTx.Len>=count+16)?16:(gUsartTx.Len-count);
							
							for(j = 0; j <sum; j++)
							{
								if(i==0)
								{
									if( data_temp&(1<<(j+gUsartTx.REG_Add%16)) )
										byte_temp |= (1<<(cnt%8));
									else
										byte_temp &=~(1<<(cnt%8));								
								}
								else
								{
									if( data_temp&(1<<j) )
										byte_temp |= (1<<(cnt%8));
									else
										byte_temp &=~(1<<(cnt%8));								
								}
								
								cnt++;
								if((cnt!=0 && cnt%8==0) || cnt>=gUsartTx.Len )
								{
									if(cnt%8==0)	
										Send_ModbusBuf[7 + cnt/8-1] = byte_temp;
									else	
										Send_ModbusBuf[7 + cnt/8] = byte_temp;
									byte_temp = 0;
								}									
							}
							count+=sum;								
						}

						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 7+Send_ModbusBuf[6]);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}					
					else if (gUsartTx.CMD == 0x10)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						Send_ModbusBuf[6] = gUsartTx.Len*2;

						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 

						for (i = 0; i < gUsartTx.Len; i++)		
						{
							// Send_ModbusBuf[7 + 2 * i + 0] = var[(gUsartTx.Var_Add) * 2 + 2 * i + 0];
							// Send_ModbusBuf[7 + 2 * i + 1] = var[(gUsartTx.Var_Add) * 2 + 2 * i + 1];
							Send_ModbusBuf[7 + 2 * i + 0] = var[(addr_temp) * 2 + 2 * i + 0];
							Send_ModbusBuf[7 + 2 * i + 1] = var[(addr_temp) * 2 + 2 * i + 1];
						}
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 7 + Send_ModbusBuf[6]);	
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}


				}
				//				else	tx_repeat_cnt = 0;
			}
			else if (gUsartTx.Mode == 0x02) // Mode 2
			{
				// uint16_t opt_addr_temp = gUsartTx.Opt_Add;
				// Var_AddrRemapping(&opt_addr_temp); 

				if (var[2 * (gUsartTx.Opt_Add)] == 0x4C && var[2 * (gUsartTx.Opt_Add) + 1] == 0x54)
				{
					if (gUsartTx.CMD == 0x01)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x02)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}										
					else if (gUsartTx.CMD == 0x03)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x04)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}					
					
					else if (gUsartTx.CMD == 0x05)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						
						num = gUsartTx.REG_Add%16;
						// data_temp = (var[(gUsartTx.Var_Add) * 2]<<8 | var[(gUsartTx.Var_Add) * 2 + 1]);					
						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 
						data_temp = (var[(addr_temp) * 2]<<8 | var[(addr_temp) * 2 + 1]);					
						Send_ModbusBuf[4] = (data_temp & (1<<num))?0xFF:0x00;
						Send_ModbusBuf[5] = 0x00;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x06)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						// Send_ModbusBuf[4] = var[(gUsartTx.Var_Add) * 2];
						// Send_ModbusBuf[5] = var[(gUsartTx.Var_Add) * 2 + 1];
						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 
						Send_ModbusBuf[4] = var[(addr_temp) * 2];
						Send_ModbusBuf[5] = var[(addr_temp) * 2 + 1];
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x0F)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						Send_ModbusBuf[6] = gUsartTx.Len%8?(gUsartTx.Len/8+1):(gUsartTx.Len/8);										
						
						num = (gUsartTx.REG_Add+gUsartTx.Len-1)/16 - gUsartTx.REG_Add/16 + 1;
						count = 0;

						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 

						for (i = 0; i < num; i++)
						{
							// data_temp = (var[(gUsartTx.Var_Add+i) * 2]<<8 | var[(gUsartTx.Var_Add+i) * 2 + 1]);
							data_temp = (var[(addr_temp+i) * 2]<<8 | var[(addr_temp+i) * 2 + 1]);
							if(i==0)
								sum = ( (gUsartTx.Len>=count+16)?16:(gUsartTx.Len-count) )-gUsartTx.REG_Add%16;
							else						
								sum = (gUsartTx.Len>=count+16)?16:(gUsartTx.Len-count);
							
							for(j = 0; j <sum; j++)
							{
								if(i==0)
								{
									if( data_temp&(1<<(j+gUsartTx.REG_Add%16)) )
										byte_temp |= (1<<(cnt%8));
									else
										byte_temp &=~(1<<(cnt%8));								
								}
								else
								{
									if( data_temp&(1<<j) )
										byte_temp |= (1<<(cnt%8));
									else
										byte_temp &=~(1<<(cnt%8));								
								}
								
								cnt++;
								if((cnt!=0 && cnt%8==0) || cnt>=gUsartTx.Len )
								{
									if(cnt%8==0)	
										Send_ModbusBuf[7 + cnt/8-1] = byte_temp;
									else	
										Send_ModbusBuf[7 + cnt/8] = byte_temp;
									byte_temp = 0;
								}									
							}
							count+=sum;								
						}

						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 7+Send_ModbusBuf[6]);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}					
					else if (gUsartTx.CMD == 0x10)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						Send_ModbusBuf[6] = gUsartTx.Len*2;

						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 

						for (i = 0; i < gUsartTx.Len; i++)			
						{
							// Send_ModbusBuf[7 + 2 * i + 0] = var[(gUsartTx.Var_Add) * 2 + 2 * i + 0];
							// Send_ModbusBuf[7 + 2 * i + 1] = var[(gUsartTx.Var_Add) * 2 + 2 * i + 1];
							Send_ModbusBuf[7 + 2 * i + 0] = var[(addr_temp) * 2 + 2 * i + 0];
							Send_ModbusBuf[7 + 2 * i + 1] = var[(addr_temp) * 2 + 2 * i + 1];
						}
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 7 + Send_ModbusBuf[6]);	
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
				}
			}
			else if (gUsartTx.Mode == 0x03) // Customized Code_ Mode 3
			{
				if (Master_mode03_flag[gUsartTx.Opt_Add] == 1)
				{
					if (gUsartTx.CMD == 0x01)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x02)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}										
					else if (gUsartTx.CMD == 0x03)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x04)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}										
					else if (gUsartTx.CMD == 0x05)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						
						num = gUsartTx.REG_Add%16;
						// data_temp = (var[(gUsartTx.Var_Add) * 2]<<8 | var[(gUsartTx.Var_Add) * 2 + 1]);					
						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 
						data_temp = (var[(addr_temp) * 2]<<8 | var[(addr_temp) * 2 + 1]);				
						Send_ModbusBuf[4] = (data_temp & (1<<num))?0xFF:0x00;
						Send_ModbusBuf[5] = 0x00;
						// var[(gUsartTx.Var_Add) * 2] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2];
						// var[(gUsartTx.Var_Add) * 2 + 1] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2 + 1];						
						var[(addr_temp) * 2] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2];
						var[(addr_temp) * 2 + 1] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2 + 1];
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x06)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2];
						Send_ModbusBuf[5] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2 + 1];
						// var[(gUsartTx.Var_Add) * 2] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2];
						// var[(gUsartTx.Var_Add) * 2 + 1] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2 + 1];
						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 
						var[(addr_temp) * 2] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2];
						var[(addr_temp) * 2 + 1] = Master_mode03_Var[(gUsartTx.Opt_Add) * 2 + 1];
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 6);
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
					else if (gUsartTx.CMD == 0x0F)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						Send_ModbusBuf[6] = gUsartTx.Len%8?(gUsartTx.Len/8+1):(gUsartTx.Len/8);										
						
						num = (gUsartTx.REG_Add+gUsartTx.Len-1)/16 - gUsartTx.REG_Add/16 + 1;
						count = 0;

						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 

						for (i = 0; i < num; i++)
						{
							// data_temp = (var[(gUsartTx.Var_Add+i) * 2]<<8 | var[(gUsartTx.Var_Add+i) * 2 + 1]);
							data_temp = (var[(addr_temp+i) * 2]<<8 | var[(addr_temp+i) * 2 + 1]);
							if(i==0)
								sum = ( (gUsartTx.Len>=count+16)?16:(gUsartTx.Len-count) )-gUsartTx.REG_Add%16;
							else						
								sum = (gUsartTx.Len>=count+16)?16:(gUsartTx.Len-count);
							
							for(j = 0; j <sum; j++)
							{
								if(i==0)
								{
									if( data_temp&(1<<(j+gUsartTx.REG_Add%16)) )
										byte_temp |= (1<<(cnt%8));
									else
										byte_temp &=~(1<<(cnt%8));								
								}
								else
								{
									if( data_temp&(1<<j) )
										byte_temp |= (1<<(cnt%8));
									else
										byte_temp &=~(1<<(cnt%8));								
								}
								
								cnt++;
								if((cnt!=0 && cnt%8==0) || cnt>=gUsartTx.Len )
								{
									if(cnt%8==0)	
										Send_ModbusBuf[7 + cnt/8-1] = byte_temp;
									else	
										Send_ModbusBuf[7 + cnt/8] = byte_temp;
									byte_temp = 0;
								}									
							}
							count+=sum;								
						}

						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 7+Send_ModbusBuf[6]);
						tx_en_flag = 0;	// The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}					
					else if (gUsartTx.CMD == 0x10)
					{
						Send_ModbusBuf[0] = gUsartTx.Sla_Add;
						Send_ModbusBuf[1] = gUsartTx.CMD;
						Send_ModbusBuf[2] = gUsartTx.REG_Add >> 8;
						Send_ModbusBuf[3] = gUsartTx.REG_Add & 0xFF;
						Send_ModbusBuf[4] = gUsartTx.Len >> 8;
						Send_ModbusBuf[5] = gUsartTx.Len & 0xFF;
						Send_ModbusBuf[6] = gUsartTx.Len*2;

						uint16_t addr_temp = gUsartTx.Var_Add;
						Var_AddrRemapping(&addr_temp); 

						for (i = 0; i < gUsartTx.Len; i++)			
						{
							// Send_ModbusBuf[7 + 2 * i + 0] = var[(gUsartTx.Var_Add) * 2 + 2 * i + 0];
							// Send_ModbusBuf[7 + 2 * i + 1] = var[(gUsartTx.Var_Add) * 2 + 2 * i + 1];
							Send_ModbusBuf[7 + 2 * i + 0] = var[(addr_temp) * 2 + 2 * i + 0];
							Send_ModbusBuf[7 + 2 * i + 1] = var[(addr_temp) * 2 + 2 * i + 1];
						}
						LT_SendData_CRC_NoFrame(Send_ModbusBuf, 7 + Send_ModbusBuf[6]);	
						tx_en_flag = 0; // The serial port will allow the sending flag bit to be cleared after sending
						pit_1ms_count3 = 0;
						timeout_flag = 1;
					}
				}
			}
		}

		Cnt_ModbusTX += 16;
	}
}
#endif


/*****************************Idle interrupt reception(uart)*****************************/
/*There is a problem temporarily. Please reserve it first*/
/*
#include "dmac_drv.h"

#define Uart_Channel	2        //DMA channel

DMA_CHANNEL_REG *sci_dma_channel[2][DMAC_CHNUM] = {{(DMA_CHANNEL_REG *)(DMA1_BASE_ADDR ),(DMA_CHANNEL_REG *)(DMA1_BASE_ADDR + 0x58 ),
									(DMA_CHANNEL_REG *)(DMA1_BASE_ADDR + 0xB0 ),(DMA_CHANNEL_REG *)(DMA1_BASE_ADDR + 0x108)},
												   {(DMA_CHANNEL_REG *)(DMA2_BASE_ADDR ),(DMA_CHANNEL_REG *)(DMA2_BASE_ADDR + 0x58 ),
									(DMA_CHANNEL_REG *)(DMA2_BASE_ADDR + 0xB0 ),(DMA_CHANNEL_REG *)(DMA2_BASE_ADDR + 0x108)}};//global struct variable for for Channel registers

DMA_CONTROL_REG *sci_dma_control[2] = {(DMA_CONTROL_REG*)(DMA1_BASE_ADDR+0x2C0),
																				(DMA_CONTROL_REG*)(DMA2_BASE_ADDR+0x2C0)};//global struct variable for for DMAC registers

uint16_t dma_uartRX(UART_TypeDef *UARTx,uint8_t n, uint8_t *dest,uint32_t len)
{
	uint8_t dmaNum = 0;
	uint16_t ret = 0;
	uint16_t temp = 0;

	if(UARTx == SCI2)
	{
		dmaNum =1;
	}
	else					//COMuart SCI3
	{
		dmaNum =0;
	}

	sci_dma_control[dmaNum]->DMA_CHEN &= (~(CHANNEL_WRITE_ENABLE(n)|CHANNEL_ENABLE(n)));////DMA disable

	sci_dma_control[dmaNum]->DMA_CONFIG = 0;
	sci_dma_channel[dmaNum][n]->DMA_SADDR = (uint32_t)&UARTx->SCIDRL;
	sci_dma_channel[dmaNum][n]->DMA_DADDR = (uint32_t)dest;
	sci_dma_channel[dmaNum][n]->DMA_CTRL = DIEC|SNOCHG|P2M_DMA;
	sci_dma_channel[dmaNum][n]->DMA_CTRL_HIGH =  len;
	sci_dma_control[dmaNum]->DMA_CONFIG = 1;

	sci_dma_channel[dmaNum][n]->DMA_CFG = (HS_SEL_SRC_HARD)|(HS_SEL_DST_SOFT)|(FIFO_EMPTY)|(CH_PRIOR2);

	if(UARTx == COMuart)
	{
		sci_dma_channel[dmaNum][n]->DMA_CFG_HIGH = (0xb<<7);
	}
	else if(UARTx == SCI2)
	{
		sci_dma_channel[dmaNum][n]->DMA_CFG_HIGH = (0x0<<7);
	}
	else
	{
		sci_dma_channel[dmaNum][n]->DMA_CFG_HIGH = (0xd<<7);
	}

	sci_dma_control[dmaNum]->DMA_CHEN |= (CHANNEL_WRITE_ENABLE(n)|CHANNEL_ENABLE(n));

	 return ret;
}

uint32_t DMA_uart_GetRecvLen(UART_TypeDef *UARTx,uint8_t n)
{
  uint8_t dmaNum = 0;

	if(UARTx == SCI2)
	{
		dmaNum =1;
	}
	else					//COMuart SCI3
	{
		dmaNum =0;
	}

	return sci_dma_channel[dmaNum][n]->DMA_CTRL_HIGH;

}

void SCI1_DMA_Init(uint32_t pclk,uint32_t bound)
{
	uint32_t band_rate=0;

	NVIC_Init(0, 0, SCI1_IRQn, 2);

	//Set baud rate: 115200
	COMuart->SCIBRDF=(((pclk*8/bound)+1)/2)&0x003f;
	band_rate =(pclk*4/bound)>>6;
	COMuart->SCIBDH  =(uint8_t)((band_rate>>8)&0x00ff);
	COMuart->SCIBDL  =(uint8_t)(band_rate&0x00ff);

	COMuart->SCICR1 &=~(1<<4);	// Data frame: 1+8+1
	COMuart->SCICR1 &=~(1<<1);	// No verification
	COMuart->SCIRXTOCTR = 64;
	COMuart->SCIFCR = 0;
	COMuart->SCIFCR  |= (SCIFCR_RFEN|SCIFCR_RXFLSEL_1_8|SCIFCR_TXFLSEL_1_8); //RX FIFO enable
	COMuart->SCIFCR2 |= (SCIFCR2_RXFCLR|SCIFCR2_RXFTOE);					 //clean fifo , time over
	COMuart->SCICR1  |=  SCICR1_ILT_MASK; 									 //Idle frame starts from end bit
	COMuart->SCICR2  |= (SCICR2_TE_MASK|SCICR2_RE_MASK|SCICR2_ILIE_MASK);	 //Idle interrupt, receive, send enable

//	SCI3->SCICR2  |= SCICR2_RE_MASK;
	COMuart->SCIDCR |= SCIDCR_RXDMAE_MASK;	//Uart_DMA enable
	dma_uartRX(COMuart,Uart_Channel, Rx_Buffer_short,256);
}

//void SCI3_IRQHandler(void)
//{
//	uint16_t tmp,ret;
//
//	UART_TypeDef *UARTx=COMuart;
//
//	//UART_Enter idle interrupt
//	if(UARTx->SCISR1 & SCISR1_IDLE_MASK)      //The receiver is idle
//	{
//		while((UARTx->SCIFSR&SCIFSR_REMPTY_MASK) == 0) //FIFO not empty, Wait for the received data to be transmitted to DMA
//		{
//			if( UARTx->SCIFSR& (SCIFSR_RFULL_MASK|SCIFSR_RTOS_MASK) )  //FIFO overflow锟斤拷receive time-out
//			{
//				break;
//			}
//		}
//
//		tmp = UARTx->SCIDRL;
//		UARTx->SCIFCR2 |= SCIFCR2_RXFCLR;

//		Rx_Count_short = DMA_uart_GetRecvLen(UARTx,Uart_Channel);
//		gUsartRx.Flag = 1;
//		printf("len:%d\r\n",Rx_Count_short);
//	//	SCI3->SCICR2  |= SCICR2_RE_MASK;
//	//	SCI3->SCIDCR |= SCIDCR_RXDMAE_MASK;	//Uart_DMA enable
//		dma_uartRX(UARTx,Uart_Channel, Rx_Buffer_short,256);
//	}

//}
*/

#if (UARTBUS_OPTION == 4)

void SPI_BUS_Initialization(void)
{
	SPI_InitTypeDef SPI_InitStruct;
	SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;
	SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
//	SPI_InitStruct.SPI_BaudRatePrescaler = SPI_Test_BaudRatePrescaler;
	SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;

	SPI_Init(SPI3,&SPI_InitStruct);
	
	SPI3->SPIRXFCR |= (1<<7);  //Reset SPI1 Rx FIFO
	SPI3->SPIRXFCR &= (~0x07); //Set Rx FIFO Threshold Value.
	SPI3->SPIRXFCR |= (1<<4);  //Enable Rx FIFO Service Threshold Interrupt
	NVIC_Init(0, 0, SPI3_IRQn, 2);
}



void SPI3_IRQHandler(void)
{
	uint8_t ch = 0;
	if((SPI3->SPISRH & 0x01) == 0x01)
	{
		ch = SPI3->SPIDR;
		if (Rx_Count_long > 4096 + 2)
			Rx_Count_long = 0;
		Rx_Buffer_long[Rx_Count_long++ % RX_SIZE] = ch;
		Rx_Num++;
		
		SPI3->SPISRH |= 0x01; //Clear Rx FIFO Service Flag
	}
	
}

#endif


#if (UARTBUS_OPTION == 5 || UARTBUS_OPTION == 6)
uint8_t Hid_Rx_Buffer_long[Hid_SIZE] = {0};
uint16_t Hid_Rx_Count_long;
uint16_t Hid_Rx_Num = 0;
uint16_t Hid_Respond_Num = 0;
uint16_t Hid_Respond_Count = 0;
uint8_t Hid_Respond_C0_OK = 0;
uint8_t Hid_Respond_C1_OK = 0;
uint8_t Hid_Respond_Length = 0;
uint8_t Hid_Respond_Flag = 0;
uint16_t Hid_Respond_FlashLength = 0;

uint8_t Hid_receive_flag = 0;
uint16_t Hid_receive_num = 0;
uint16_t Hid_receive_count = 0;

void Hid_Main_From_UartRxBuff(void)
{
	uint16_t i = 0;
	if (Hid_Respond_Num < Hid_Rx_Num)
	{
		
		for (i = 0; i < (Hid_Rx_Num - Hid_Respond_Num); i++)
		{
			if (Hid_Respond_C1_OK)
			{
				gHidRx.Buf[gHidRx.Count++] = Hid_Rx_Buffer_long[(i + Hid_Respond_Num) % Hid_SIZE];

				if (Hid_Respond_Flag == 1)
				{
					Hid_Respond_Flag = 0;
					Hid_Respond_Length = Hid_Rx_Buffer_long[(i + Hid_Respond_Num) % Hid_SIZE];

					if (Hid_Respond_Length == 0)
					{
						Hid_Respond_Flag = 2;
					}
				}
				else if (Hid_Respond_Length == 0)
				{
					if (Hid_Respond_Flag == 2)
					{
						Hid_Respond_FlashLength = Hid_Rx_Buffer_long[(i + Hid_Respond_Num) % Hid_SIZE];
						Hid_Respond_Flag = 3;
					}
					else if(Hid_Respond_Flag == 3)
					{
						Hid_Respond_FlashLength = (Hid_Respond_FlashLength << 8) + Hid_Rx_Buffer_long[(i + Hid_Respond_Num) % Hid_SIZE] + 2;
						Hid_Respond_Flag = 0;

					}
				}
				
				if ((gHidRx.Count > Hid_Respond_Length && Hid_Respond_Length != 0) || (gHidRx.Count > Hid_Respond_FlashLength && Hid_Respond_Length == 0 && Hid_Respond_Flag == 0))
				{

					gHidRx.Flag = 1;
					Hid_Respond_C1_OK = 0;
					Hid_Respond_C0_OK = 0;
					break;
				}
			}
			if (Hid_Respond_C0_OK && !Hid_Respond_C1_OK && Hid_Rx_Buffer_long[(i + Hid_Respond_Count) % Hid_SIZE] != SCI_C1)
			{
				Hid_Respond_C0_OK = 0;
				gHidRx.Count = 0;
			}

			if (!Hid_Respond_C0_OK && Hid_Rx_Buffer_long[(i + Hid_Respond_Count) % Hid_SIZE] == SCI_C0)
			{
				Hid_Respond_C0_OK = 1;
				gHidRx.Count = 0;
			}
			if (!Hid_Respond_C1_OK && Hid_Rx_Buffer_long[(i + Hid_Respond_Count) % Hid_SIZE] == SCI_C1 && Hid_Respond_C0_OK)
			{
				Hid_Respond_C1_OK = 1;
				Hid_Respond_Flag = 1;
				gHidRx.Count = 0;
			}


		}
		Hid_Respond_Count = (i + Hid_Respond_Count) % Hid_SIZE;
		Hid_Respond_Num += i;

	}
	else if (Hid_Respond_Num == Hid_Rx_Num && Hid_Rx_Num != 0)
	{

		Hid_Respond_Num = 0;
		Hid_Rx_Num = 0;
		Hid_Respond_Count = 0;
		Hid_Rx_Count_long = 0;
	}
}
#endif
